import abc
from typing import Generic, TypeVar


class AttachedJob(abc.ABC):
    @property
    @abc.abstractmethod
    def active(self) -> bool:
        pass

    @abc.abstractmethod
    def terminate(self) -> bool:
        pass

    def __enter__(self) -> 'AttachedJob':
        if not self.active:
            raise RuntimeError(f"The job is not active.")
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.active:
            self.terminate()

    def __del__(self):
        if self.active:
            self.terminate()


C = TypeVar('C')
J = TypeVar('J', bound=AttachedJob)


class AttachedJobsPlatform(Generic[C, J], abc.ABC):
    @abc.abstractmethod
    def new_attached_job(self, config: C) -> AttachedJob:
        pass
